package com.zeny.coding.controller.admin.system;


import com.zeny.coding.constants.CourseConstants;
import com.zeny.coding.dto.PageDto;
import com.zeny.coding.dto.Response;
import com.zeny.coding.dto.RoleDto;
import com.zeny.coding.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <h2>角色</h2>
 *
 * @Author Zeny
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/system/admin/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("/list")
    public Response list(@RequestBody PageDto<RoleDto> pageDto) {
        pageDto.check();
        roleService.list(pageDto);
        return new Response().success().data(pageDto).message(CourseConstants.QUERY_SUCCESS);
    }

    @PostMapping("/save")
    public Response save(@RequestBody RoleDto roleDto) {
        roleDto.isValidForSave();
        this.roleService.save(roleDto);
        return new Response().success().message(CourseConstants.SAVE_SUCCESS);
    }

    @PutMapping("/update")
    public Response update(@RequestBody RoleDto roleDto) {
        roleDto.isValidForUpdate();
        this.roleService.update(roleDto);
        return new Response().success().message(CourseConstants.UPDATE_SUCCESS);
    }

    @GetMapping("/findById/{id}")
    public Response findById(@PathVariable("id") String id) {
        if (StringUtils.isEmpty(id)) {
            return new Response().fail().message(CourseConstants.REQUEST_PARAMS_ERROR);
        }
        RoleDto roleDto = this.roleService.findById(id);
        return new Response()
                .success()
                .data(roleDto)
                .message(CourseConstants.QUERY_SUCCESS);
    }

    @DeleteMapping("/deleteById/{id}")
    public Response deleteById(@PathVariable("id") String id) {
        if (StringUtils.isEmpty(id)) {
            return new Response().fail().message(CourseConstants.REQUEST_PARAMS_ERROR);
        }
        this.roleService.deleteById(id);
        return new Response().success().message(CourseConstants.DELETE_SUCCESS);
    }

    /**
     * <h2>根据用户id查询用户和角色的分配关系</h2>
     * @param id
     **/
    @GetMapping("/findAssignRoleRelation/{id}")
    public Response findAssignRoleRelation(@PathVariable("id") String id) {
        Map<String, List<RoleDto>> map = this.roleService.findAssignRoleRelation(id);
        return new Response().success().data(map);
    }
}
